Get "Camera Target" using the API

I’m trying to get the absolute values for my active camera.
So far I got the Pivot and Position Vector objects right but I can’t seem to find anything referencing the Target

Here’s a snippet of my code so far:

import lux
import luxmath

def move_model_and_adjust_camera(model_name, camera_name, new_model_center_xyz):
    # Get the model and camera objects
    # `find()` returns an itterable, so we want to find the first match
    model = lux.getSceneTree().find(name=model_name)[0]
    lux.setCamera(camera_name)
    
    # Get the min and max values for the models's bounding box
    model_boundingbox = model.getBoundingBox()

    # Unpack the `Vector` and calculate it's the center of the bounding box
    # This is acheived by calculating the average for every coordinate
    min_vertex, max_vertex = model_boundingbox
    model_center_x = round((min_vertex.x + max_vertex.x) / 2, 1)
    model_center_y = round((min_vertex.y + max_vertex.y) / 2, 1)
    model_center_z = round((min_vertex.z + max_vertex.z) / 2, 1)

    model_center = luxmath.Vector(model_center_x, model_center_y, model_center_z)
    
    camera_position = luxmath.Vector(lux.getCameraPosition())
    camera_target = luxmath.Vector(lux.getCameraDirection())
    camera_pivot = luxmath.Vector(lux.getCameraLookAt())
    
    # DEBUG PRINT
    print(camera_position, camera_target, camera_pivot)

I’m trying to find the right method for camera_target, getCameraDirection and getCameraDistance dont seem to be the right ones.

For example:
imagem
I can only find 2 of the methods that return the values I’m looking for.

Can someone help me please?

You have to toggle off the keep view direction option

Hey,
in most cases i would expect the Pivot and target to be the same when working with scripting, but i can see that this might not always be the case. I will check with the responsibles why there is no explicit method to get the camera target.

As a workaround you can use a little math to calculate it.
The Camera direction vector is normalized and gives the vector relative to the camera. For that reason it is not matching what you see in the UI.

You can get the original value if you take the camera direction vector and multiply it with the camera Distance. Then you should get the direction vector relative to the camera postion.

You can then add that vector to the absolute camera Position vector you already have.
The result should be the absolute camera target vector.

This would be the resulting code:
luxmath.Vector(lux.getCameraPosition()).add(luxmath.Vector(lux.getCameraDirection()).mul(lux.getCameraDistance()))

I hope this helps.

1 Like

Hey Niko, thank you for getting back to me on this.

in most cases i would expect the Pivot and target to be the same when working with scripting, but i can see that this might not always be the case.

Yes, this definitely an edge case. It was completely unintended though, and there’s no real consequence to my current project if I keep Target and Pivot the same as the old camera’s configuration. It was just the consequence of manually fiddling with the camera until I got a satisfactory result.
At this point, it’s mostly just a challenge…

I should say that my current script is attempting to center a model in the environment and move the active camera by the same delta, so that I can undo whatever mess I did when I first setup the scene and keep the camera intact, so to say.
I’ll share it once it’s finished.

This would be the resulting code:
luxmath.Vector(lux.getCameraPosition()).add(luxmath.Vector(lux.getCameraDirection()).mul(lux.getCameraDistance()))

Yeah these are the values I get for the Target, thank you!

Now my only real question is: “How do I get to set the Target to be a different value from the Pivot?”

Before your reply, I was able to figure out that the target was related to cameraDistance and cameraDirection and thought that maybe if I made sure that those two attributes were the same then the Target would be different from the Pivot.
I was able to move the camera and model successfully but even after trying setCameraDistance and setCameraDirection to the previous camera’s values (even though they didn’t change) the Target and Pivot remained the same.

Hey,
I am not sure if you can set the target and pivot to different values via scripting.
Do you have an actual need for this or is it curriosity at this point?

Usually the way you can end up with missmatched Pivot and Target is when using the “Pan” camera Navigation method. Though i am not entirely certain that there are no other ways to end up in that situation.
You may have noticed that after using “pan” the camera will no longer rotate around the Lookat Point.
If that is undesired you can prevent it by enabling “Use Target as Pivot” in the cameras Position and Orientation settings.

I am not sure if you can set the target and pivot to different values via scripting.
Do you have an actual need for this or is it curiosity at this point?

Yes, this is just curiosity and the challenge of replicating a camera that I’ve manually created as much as possible. In this specific case, having the different Pivot and Target values is just about the same as translating the frame by x: -2px and y: 5px in an image editor.

I ran into some difficulties with awkward HDRI pin placement while my models’ bounding box were aligned to -x, y:0, -z but I was already too deep into rendering with the camera I’ve manually created and that alignment.
The combination of all the camera values relative to the model’s center must be absolutely the same to maintain the same proportions to previous images, scripting this change would be the only way.